﻿Public Class DialogLokasi
    Dim arrField As String() = New String() {"koderetail", "namaretail", "alamat", "telp"}
    Private Function getValParam(ByVal value As String) As Object
        Dim arrCtl() As Control
        Dim retval As String = ""

        arrCtl = Me.Controls.Find(value, True)
        If arrCtl.Length > 0 Then
            If TypeOf arrCtl(0) Is TextBox Then
                Dim txb As TextBox = CType(arrCtl(0), TextBox)
                retval = txb.Text
            End If
        End If
        Return retval
    End Function
    Private Function setParam(ByVal opt As String) As Object()
        Dim params As New List(Of Object)
        params.Add(opt)
        For Each strField As String In arrField
            params.Add(getValParam(strField))
        Next
        Return params.ToArray()
    End Function
    Private Sub loaddata()
        Dim dt As New DataTable
        If Utils.executeSP("proc_lokasiretail", setParam("select"), dt) Then
            Utils.setgrid(DGView, Nothing, dt)
        End If
    End Sub
    Private Sub refreshForm(ByVal meparent As Control)
        For Each ctl As Control In meparent.Controls
            If ctl.Controls.Count > 0 Then
                refreshForm(ctl)
            End If
            If TypeOf ctl Is MBGlassStyleButton.MBGlassButton Then
                ctl.Enabled = True
                ctl.Text = ctl.Tag
            ElseIf TypeOf ctl Is TextBox Then
                ctl.Enabled = True
                ctl.Text = ""
            ElseIf TypeOf ctl Is DataGridView Then
                ctl.Enabled = True
                loaddata()
            End If

        Next
        PanelCtl.Enabled = False
    End Sub
    Private Sub ClearCtl()
        For Each ctl As Control In PanelCtl.Controls
            If TypeOf ctl Is TextBox Then
                ctl.Text = ""
            End If
        Next
    End Sub
    Private Sub setValCtl(ByVal ctlname As String, ByVal value As String)
        Dim arrCtl() As Control
        arrCtl = Me.Controls.Find(ctlname, True)
        If arrCtl.Length > 0 Then
            If TypeOf arrCtl(0) Is TextBox Then
                Dim txb As TextBox = CType(arrCtl(0), TextBox)
                txb.Text = value
            End If
        End If

    End Sub
    Private Sub DialogLokasi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        refreshForm(Me)
        'loaddata()
    End Sub

    Private Sub MbGlassButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton3.Click
        Me.Close()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        refreshForm(Me)

    End Sub

    Private Sub DGView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGView.CellContentClick
        Dim dgr As New DataGridViewRow
        If e.RowIndex < 0 Then
            Exit Sub
        End If
        dgr = DGView.Rows(e.RowIndex)
        For Each col As DataGridViewColumn In DGView.Columns
            setValCtl(col.Name, dgr.Cells(col.Name).Value.ToString())
        Next
    End Sub

    Private Sub DGView_RowStateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles DGView.RowStateChanged
        If e.Row.Index = -1 Then
            Exit Sub
        End If
        For Each col As DataGridViewColumn In DGView.Columns
            setValCtl(col.Name, e.Row.Cells(col.Name).Value.ToString())
        Next
    End Sub
    Sub setButton(ByVal btnname)
        For Each ctl As Control In Panel4.Controls
            If TypeOf ctl Is MBGlassStyleButton.MBGlassButton Then
                If ctl.Text.ToLower = "refresh" Then
                    ctl.Enabled = True
                ElseIf ctl.Text.ToLower = "close" Then
                    ctl.Enabled = True
                Else
                    ctl.Text = ctl.Tag
                    If ctl.Name = btnname Then
                        ctl.Enabled = True
                        ctl.Text = "Save"
                    Else
                        ctl.Enabled = False
                    End If
                End If

            End If
        Next
    End Sub
    Function validasi() As Boolean
        Dim retval As Boolean = True
        For Each ctl As Control In PanelCtl.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Text = "" Then
                    retval = False
                    Exit For
                End If
            End If
        Next
        Return retval
    End Function
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim btn As MBGlassStyleButton.MBGlassButton = CType(sender, MBGlassStyleButton.MBGlassButton)
        If Not btn.Text.ToLower = "save" Then
            ClearCtl()
            setButton(btn.Name)
            DGView.Enabled = False
            PanelCtl.Enabled = True
        Else
            'validasi
            If Not validasi() Then
                Utils.getmessagesystem(1, "Data Entri Tidak Lengkap")
            Else
                'simpan
                Dim dt As New DataTable
                If Utils.executeSP("proc_lokasiretail", setParam("add"), dt) Then
                    If Utils.RowExist(dt) Then
                        Utils.getmessagesystem(0, dt(0)(0))
                    End If
                End If
            End If

            
            'refresh
            refreshForm(Me)
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim btn As MBGlassStyleButton.MBGlassButton = CType(sender, MBGlassStyleButton.MBGlassButton)
        If Not btn.Text.ToLower = "save" Then
            setButton(btn.Name)
            PanelCtl.Enabled = True
            koderetail.Enabled = False
        Else

            If Not validasi() Then
                Utils.getmessagesystem(1, "Data Entri Tidak Lengkap")
            Else
                'simpan
                Dim dt As New DataTable
                If Utils.executeSP("proc_lokasiretail", setParam("edit"), dt) Then
                    If Utils.RowExist(dt) Then
                        Utils.getmessagesystem(0, dt(0)(0))
                    End If
                End If
            End If


            'refresh
            refreshForm(Me)
        End If
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If Not validasi() Then
            Utils.getmessagesystem(1, "Data Entri Tidak Lengkap")
        Else
            'confirmasi
            Dim retval As Integer = MessageBox.Show("Apakah anda yakin akan menghapus " & namaretail.Text & " ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
            If retval = DialogResult.Yes Then
                'simpan
                Dim dt As New DataTable
                If Utils.executeSP("proc_lokasiretail", setParam("delete"), dt) Then
                    If Utils.RowExist(dt) Then
                        Utils.getmessagesystem(0, dt(0)(0))
                    End If
                End If

            End If
            
        End If


        'refresh
        refreshForm(Me)
    End Sub
End Class